package com.glorystone.ins.service.impl;

import com.glorystone.ins.common.common.PageVo;
import com.glorystone.ins.dao.BusinessLogMapper;
import com.glorystone.ins.dao.RepayingMapper;
import com.glorystone.ins.domain.BusinessLog;
import com.glorystone.ins.domain.DepositRecord;
import com.glorystone.ins.domain.Repaying;
import com.glorystone.ins.enums.CapitalRecordStatusEnum;
import com.glorystone.ins.enums.MainOrderStatusEnum;
import com.glorystone.ins.interceptor.PageList;
import com.glorystone.ins.service.CreditTotalService;
import com.glorystone.ins.service.OrderReapyingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author:LiuSC
 * @Date:Created in 2018-03-20 10:53
 * @since: JDK 1.7
 * @Description:
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class OrderReapyingServiceImpl implements OrderReapyingService {

    @Autowired
    private RepayingMapper repayingMapper;
    @Autowired
    private BusinessLogMapper businessLogMapper;
    @Autowired
    private CreditTotalService creditTotalService;

    @Override
    public PageList<Repaying> queryRepayList(PageVo vo) {
        return repayingMapper.queryRepayList(vo,vo.getPageable());
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean updateOrder(Repaying repay) {
        int i =repayingMapper.updateOrder(repay);
        return i==0 ? false : true;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean updateCapital(Repaying repay) {
        int i =repayingMapper.updateCapital(repay);
        int status = CapitalRecordStatusEnum.SETTLED.getValue();
        int cs = repay.getCapitalStatus();
        int capitalSourcr = 9527;
        if(repay.getCapitalSourcr()!=null){
            capitalSourcr = repay.getCapitalSourcr();
        }
        String context = BusinessLog.ContentEnum.REPAYING_INSURER_ORDER.getValue();
        if(status==cs){
            Repaying rp = new Repaying();
            rp.setTid(repay.getTid());
            rp.setChannelStatus(MainOrderStatusEnum.HAS_SURRENDER_BALANCE.getIndex());
            repayingMapper.updateOrder(rp);
            context = BusinessLog.ContentEnum.REPAYING_CHANNEL_ORDER.getValue();
        }else{
            Repaying rp = new Repaying();
            rp.setTid(repay.getTid());
            rp.setSurrenderDate (repay.getSurrenderDate());
            repayingMapper.updateOrder(rp);
        }
        if(2==capitalSourcr){
            BigDecimal rp = repay.getRealPremium();
            BigDecimal ra = repay.getRealRebateAmt();
            if(rp == null){
                rp = new BigDecimal(0);
            }
            if(ra == null){
                ra = new BigDecimal(0);
            }
            BigDecimal all = rp.add(ra);
            creditTotalService.operateDeposit(repay.getChannelId(), DepositRecord.TypeEnum.RETURN_DEPOSIT,all,repay.getRemark());
        }
        BusinessLog log = BusinessLog.builder()
                .businessType(BusinessLog.BusinessTypeEnum.ORDER.getValue())
                .businessId(repay.getTid())
                .content(context)
                .remark(repay.getRemark())
                .createBy(repay.getCapitalUpdateBy()).build();
        businessLogMapper.save(log);
        return i==0 ? false : true;
    }

    @Override
    public List<Repaying> getRepayList(Map<String,String> map) {
        return repayingMapper.getRepayList(map);
    }
}
